function h = generateHist2DPoint(img, T, direction)
    if nargin < 3
        error('Wrong number of arguments.');
    end
    if T <= 0
        error('Threshold must be above 0.');
    end
    zval = [];
    zval.h = [0, 0, 0, 1];
    zval.v = [0, 0, 1, 0];
    zval.m = [0, 0, 1, 1];
    zval.r = [0, 1, 1, 0];
    switch direction
        case 'h' %horizontal
            h = generateHist2D_h(img, T);
        case 'v' %vertical
            h = generateHist2D_v(img, T);
        case 'm' %'main diagonal'
            h = generateHist2D_md(img, T);
        case 'r' %'reverse diagonal'
            h = generateHist2D_rd(img, T);
        otherwise
            error('Unknown direction');
    end
    for i = 1:2*T+1
        p = sum(h(i,:));
        if p > 0
            h(i,:) = h(i, :) / p;
        end
    end
end

function h=generateHist2D_d(img, T)
end

function h=generateHist2D_h(img, T)
    [M,N] = size(img);
    h = zeros(2*T+1, 2*T+1);
    for i = 1:M
        for j = 1:N-1
            u1 = img(i,j);
            u2 = img(i,j+1);
            h(u1+T+1, u2+T+1) = h(u1+T+1, u2+T+1) + 1;
        end
    end
end

function h=generateHist2D_v(img, T)
    [M,N] = size(img);
    h = zeros(2*T+1, 2*T+1);
    for i = 1:M-1
        for j = 1:N-1
            u1 = img(i,j);
            u2 = img(i+1,j+1);
            h(u1+T+1, u2+T+1) = h(u1+T+1, u2+T+1) + 1;
        end
    end
end

function h=generateHist2D_md(img, T)
    [M,N] = size(img);
    h = zeros(2*T+1, 2*T+1);
    for i = 1:M-1
        for j = 1:N-1
            u1 = img(i+1,j);
            u2 = img(i,j+1);
            h(u1+T+1, u2+T+1) = h(u1+T+1, u2+T+1) + 1;
        end
    end
end

function h=generateHist2D_rd(img, T)
    [M,N] = size(img);
    h = zeros(2*T+1, 2*T+1);
    for i = 1:M-1
        for j = 1:N
            u1 = img(i,j);
            u2 = img(i+1,j);
            h(u1+T+1, u2+T+1) = h(u1+T+1, u2+T+1) + 1;
        end
    end
end
